package com.hl.testdb.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDiskIOException;
import android.util.Log;
import android.widget.Toast;

import com.hl.testdb.MyApplication;

public final class DBOperator{

    private SQLiteDatabase db;


    public DBOperator()
    {
        try {
            db = new DBHelper(MyApplication.getInstance()).getReadableDatabase();
            db.enableWriteAheadLogging();
            Log.d("Tag", "db isWriteAheadLoggingEnabled: "+db.isWriteAheadLoggingEnabled());
        }catch (Exception e)
        {
            e.printStackTrace();
            if (e instanceof SQLiteDiskIOException)
            {
                Toast.makeText(MyApplication.getInstance(),"No available space in disk.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public boolean isDbNull()
    {
        return db == null;
    }

    public void insert(ContentValues contentValues)
    {
        db.insert("person",null,contentValues);
    }

    public Cursor selectAll()
    {
        return db.query("person",null,null,null,null,null,null);
    }


    public void close()
    {
        if (db != null) db.close();
    }
}
